package com.eric.windows.imgDispose.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.eric.windows.R;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EView;

/**
 * <h3>Description</h3> 扭曲图像
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/8/29 14:31
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLIT  Co., Ltd. Inc. All rights reserved.
 */
@EView
public class BitmapMeshView extends View {

    /** 定义两个常量，指定在横轴、竖轴划分20格*/
    private final int WIDTH = 200;
    private final int HEIGHT = 200;
    /** 图像顶点数*/
    private final int COUNT = (WIDTH + 1) * (HEIGHT + 1);
    /** 定义一个数组 保存bitmap的顶点位置 */
    private  float[] verts = new  float[COUNT * 2];
    /** 定一个数组，记录bitmap扭曲后的顶点坐标*/
    private  float[] orig = new  float[COUNT * 2];
    /** 图片资源*/
    private Bitmap bitmap;

    public BitmapMeshView(Context context) {
        super(context);
    }

    public BitmapMeshView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BitmapMeshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BitmapMeshView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * 该方法将会在每个构造函数中调用
     */
    @AfterInject
    public void initData(){
        setFocusable(true);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic_2);

        //获取bitmap的高度和宽度
        float bitmapWidth = bitmap.getWidth();
        float bitmapHeight = bitmap.getHeight();

        int index = 0;

        //初始化顶点坐标
        for (int y = 0; y < HEIGHT; y++) {
            float fy = bitmapHeight * y / HEIGHT;
            for (int x = 0; x < WIDTH; x++) {
                float fx = bitmapWidth * x /WIDTH;
                orig[index*2 + 0] = verts[index*2 + 0] = fx;
                orig[index*2 + 1] = verts[index*2 + 1] = fy;
                index += 1 ;
            }
        }

        setBackgroundColor(Color.WHITE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        //设置从第1个点开始扭曲（第五个参数设置为0）
        canvas.drawBitmapMesh(bitmap,WIDTH,HEIGHT,verts,0,null,0,null);
    }

    /**
     * 图像被触摸时，计算verts各个元素的值
     * @param cx
     * @param cy
     */
    private void warp(float cx ,float cy){
        for (int i = 0; i < COUNT * 2; i += 2) {
            //计算顶点到（cx，cy）的距离
            float dx = cx - orig[i + 0];
            float dy = cy - orig[i + 1];
            float dd = dx * dx + dy * dy;
            float d = (float) Math.sqrt(dd);
            //计算扭曲度 80000 是随意给出的值
            float pull = 80000 / ((float)(dd * d ));

            if (pull >= 1){
                verts[i + 0] = cx;
                verts[i + 1] = cy;
            }else {
                verts[i + 0] = orig[i + 0] +  dx * pull;
                verts[i + 1] = orig[i + 1] +  dy * pull;
            }

        }

        invalidate();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        warp(event.getX(),event.getY());
        return true;
    }




}
